package logic

import (
	"context"
	"errors"
	"short-vid-joy/models/mysql"
	"strings"

	"short-vid-joy/shortrpc/internal/svc"
	"short-vid-joy/shortrpc/shortrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *shortrpc.CommentAddRequest) (*shortrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User

	if err := findUser.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	var video mysql.Video

	if err := video.FindVideoById(mysql.DB, in.VideoId); err != nil {
		return nil, errors.New("视频不存在")
	}

	if in.Content == "" {
		return nil, errors.New("请输入评论内容")
	}

	if strings.ContainsAny(in.Content, "垃圾,骗人") {
		return nil, errors.New("含违规词汇，请修改")
	}

	comment := &mysql.Comment{
		UserId:      in.UserId,
		VideoId:     in.VideoId,
		Content:     in.Content,
		IsAnonymous: in.IsAnonymous,
	}

	if err := comment.CreateComment(mysql.DB); err != nil {
		return nil, err
	}

	return &shortrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
